package com.central.apps.handler.supplier;

import com.central.app.common.base.BaseHandler;
import com.central.app.service.HandlerService;
import com.central.bizcommon.model.flow.FlowNode;
import com.central.bizcommon.model.flow.FlowOpinion;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

//供应商台账修改
@Component
@Slf4j
public class SupplierModificationHandler extends BaseHandler {
    @Autowired
    private HandlerService handlerService;

    @Override
    public void afterExcuteFlow(String app, Long id, FlowNode flowNode, FlowOpinion op, String event) throws Exception {
        if (flowNode.getId().equals("approvaler")) {
            boolean flag = op.isFlag();
            if (flag){
                List<Map<String, Object>> bySql = handlerService.findBySql("select * from gq_modification where id=" + id);
               if (CollectionUtils.isNotEmpty(bySql)){
                   for (int i=0;i<bySql.size();i++){
                       Map<String, Object> map = bySql.get(i);
                       String representative = MapUtils.getString(map, "representative");
                       String deposits = MapUtils.getString(map, "deposits");
                       String licenses = MapUtils.getString(map, "licenses");
                       String addresss = MapUtils.getString(map, "addresss");
                       String accounts = MapUtils.getString(map, "accounts");
                       String businesss = MapUtils.getString(map, "businesss");
                       String safetys = MapUtils.getString(map, "safetys");
                       String qualifications = MapUtils.getString(map, "qualifications");
                       String professionals = MapUtils.getString(map, "professionals");
                       String contacts = MapUtils.getString(map, "contacts");
                       String numbers = MapUtils.getString(map, "numbers");
                       String safetyqs = MapUtils.getString(map, "safetyqs");
                       String faxs = MapUtils.getString(map, "Faxs");
                       String exblacklists = MapUtils.getString(map, "exblacklists");
                       String supplierno = MapUtils.getString(map, "supplierno");
                       if (exblacklists==null){
                           handlerService.updateBySql("UPDATE gq_account \n" +
                                   "SET legal = '"+representative+"' \n" +
                                   ", deposit = '"+deposits+"' \n" +
                                   ", license = '"+licenses+"' \n" +
                                   ", address = '"+addresss+"' \n" +
                                   ", account = '"+accounts+"' \n" +
                                   ", business = '"+businesss+"' \n" +
                                   ", safety = '"+safetys+"' \n" +
                                   ", qualification = '"+qualifications+"' \n" +
                                   ", professional = '"+professionals+"' \n" +
                                   ", contacts = '"+contacts+"' \n" +
                                   ", number = '"+numbers+"' \n" +
                                   ", safetyq = '"+safetyqs+"' \n" +
                                   ", Fax = '"+faxs+"' \n" +
                                   ", exblacklist = "+exblacklists+" \n" +
                                   "WHERE\n" +
                                   "\tsupplierno = '"+supplierno+"'");

                       }else {
                           handlerService.updateBySql("UPDATE gq_account \n" +
                                   "SET legal = '"+representative+"' \n" +
                                   ", deposit = '"+deposits+"' \n" +
                                   ", license = '"+licenses+"' \n" +
                                   ", address = '"+addresss+"' \n" +
                                   ", account = '"+accounts+"' \n" +
                                   ", business = '"+businesss+"' \n" +
                                   ", safety = '"+safetys+"' \n" +
                                   ", qualification = '"+qualifications+"' \n" +
                                   ", professional = '"+professionals+"' \n" +
                                   ", contacts = '"+contacts+"' \n" +
                                   ", number = '"+numbers+"' \n" +
                                   ", safetyq = '"+safetyqs+"' \n" +
                                   ", Fax = '"+faxs+"' \n" +
                                   ", exblacklist = '"+exblacklists+"' \n" +
                                   "WHERE\n" +
                                   "\tsupplierno = '"+supplierno+"'");
                       }

                   }
                   //同步文件
                   synchronizationFile(bySql);
               }
            }
        }

        super.afterExcuteFlow(app, id, flowNode, op, event);
    }

    /**
     * 同步文件
     * @param bySql
     */
    private void synchronizationFile(List<Map<String, Object>> bySql) throws Exception {
        Map<String, Object> map = bySql.get(0);
        String id = MapUtils.getString(map, "id");
        String supplierno = MapUtils.getString(map, "supplierno");
        String sql ="select * from tb_file where recordid = '" + id + "' and appname = 'modificationApp' and isshow = '1'";
        List<Map<String, Object>> bySql1 = handlerService.findBySql(sql);
        if(CollectionUtils.isNotEmpty(bySql1)){
            for (Map<String, Object> stringObjectMap : bySql1) {
                stringObjectMap.put("fileid",null);
                stringObjectMap.put("appname","accountApp");
                List<Map<String, Object>> bySql2 = handlerService.findBySql("select id from gq_account where supplierno = '" + supplierno + "'");
                if(CollectionUtils.isNotEmpty(bySql2)){
                    stringObjectMap.put("recordid",MapUtils.getString(bySql2.get(0),"id"));
                }
                handlerService.insert("tb_file",stringObjectMap);
            }
        }
    }
}
